/***
 * Brian Hession -- February 16, 2013
 * 
 * PlayerWorld
 *
 * Changelog
 * Date				Changes
 * 2/16/2013		Initial version
 *
 */

#include "playerworld.h"

PlayerWorld::PlayerWorld() {
	position.x = 0;
	position.y = 0;
	dir = RIGHT;
}

void PlayerWorld::init() {
	SDL_Rect* rects = new SDL_Rect[16];
	for( int i = 0; i < 4; ++i ) {
		for( int j = 0; j < 4; ++j ) {
			rects[i*4+j].x = 32*j;
			rects[i*4+j].y = 32*i;
			rects[i*4+j].w = 32;
			rects[i*4+j].h = 32;
		}
	}
	a.setNumClips(4);
	a.setLength(4);
	a.setDuration(500);
	a.setImages(rects);
	a.setClip(dir);
	image = Graphics::loadImage( "graphics/brianworld.bmp" );
}

void PlayerWorld::setPosition( int x, int y ) {
	position.x = x;
	position.y = y;
}

void PlayerWorld::setPosition( SDL_Rect rect ) {
	position = rect;
}

void PlayerWorld::up() {
	dir = UP;
}

void PlayerWorld::right() {
	dir = RIGHT;
}

void PlayerWorld::left() {
	dir = LEFT;
}

void PlayerWorld::down() {
	dir = DOWN;
}

SDL_Rect PlayerWorld::getPosition() {
	return position;
}

void PlayerWorld::draw( SDL_Surface* screen ) {
	Graphics::applySurface(&position,image,screen,a.getImage());
}

void PlayerWorld::calculate( int delta ) {
	a.setTimestamp( a.getTimestamp() + delta );
	if( a.getTimestamp() > a.getDuration()/a.getLength() - 5 ) {
		a.step();
		a.setTimestamp(0);
	}
	switch(dir) {
		case RIGHT:
			position.x += 1;
			if( position.x >= 600-33 ) {
				dir = DOWN;
				a.setClip(dir);
			}
			break;
		case LEFT:
			position.x -= 1;
			if( position.x <= 1 ) {
				dir = UP;
				a.setClip(dir);
			}
			break;
		case UP:
			position.y -= 1;
			if( position.y <= 1 ) {
				dir = RIGHT;
				a.setClip(dir);
			}
			break;
		case DOWN:
			position.y += 1;
			if( position.y >= 480-33 ) {
				dir = LEFT;
				a.setClip(dir);
			}
			break;
	}
}

